package com.atguigu.eduservice.service.impl;

import com.atguigu.eduservice.entity.EduChapter;
import com.atguigu.eduservice.entity.EduVideo;
import com.atguigu.eduservice.entity.chapter.ChapterVo;
import com.atguigu.eduservice.entity.chapter.VideoVo;
import com.atguigu.eduservice.mapper.EduChapterMapper;
import com.atguigu.eduservice.service.EduChapterService;
import com.atguigu.eduservice.service.EduVideoService;
import com.atguigu.servicebase.exceptionhandler.GuliException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sun.org.apache.bcel.internal.generic.NEW;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 章节 服务实现类
 * </p>
 *
 * @author qing
 * @since 2021-08-01
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {
    @Resource
    private EduVideoService eduVideoService;


    @Override
    public List<ChapterVo> getChapterVideoByCourseId(String courseId) {
        //1.根据课程id 查出课程里面所有章节
        QueryWrapper<EduChapter> eduChapterQW = new QueryWrapper<>();
        eduChapterQW.eq("course_id",courseId);
        //根据课程id查询到所有的章节
        List<EduChapter> eduChapterList = baseMapper.selectList(eduChapterQW);

        //2.查询小节
        QueryWrapper<EduVideo> eduVideoQW = new QueryWrapper<>();
        eduVideoQW.eq("course_id",courseId);
        //根据课程id查询小节数据
        List<EduVideo> eduVideoList = eduVideoService.list(eduVideoQW);

        //最终返回的list集合
        List<ChapterVo> finalList = new ArrayList<>();

        //遍历查询章节list集合
        //3.遍历章节list集合进行封装
        for (EduChapter eduChapter : eduChapterList) {

            ChapterVo chapterVo = new ChapterVo();
            BeanUtils.copyProperties(eduChapter,chapterVo);
            //放到finalList中
            finalList.add(chapterVo);

            //4.遍历查询小节list集合进行封装
            List<VideoVo> videoVoList = new ArrayList<>();
            for (EduVideo eduVideo : eduVideoList) {
                //eduVideo是得到的每个小节
                //判断小节中的章节id和章节id一样就封装
                if (eduChapter.getId().equals(eduVideo.getChapterId())){
                    VideoVo videoVo = new VideoVo();
                    BeanUtils.copyProperties(eduVideo,videoVo);
                    videoVoList.add(videoVo);

                }

            }
            //把封装之后小节list集合，放到章节对象中去
            chapterVo.setChildren(videoVoList);
        }

        return finalList;
    }

    @Override
    public boolean deleteChapter(String chapterId) {

        //1.根据章节id查询小节,如果有小节不能删除
        QueryWrapper<EduVideo> eduVideoQW = new QueryWrapper<>();
        eduVideoQW.eq("chapter_id",chapterId);
        //根据条件查询记录数
        int count = eduVideoService.count(eduVideoQW);

        if (count > 0 ){ //查出数据不删除
            throw new GuliException(20001,"该章节下有数据不能删除");
        }else { //没数据删除
            int result = baseMapper.deleteById(chapterId);
            return result>0;
        }



    }

    @Override
    public void removeChapterByCorseId(String courseId) {
        QueryWrapper<EduChapter> eduChapterQW = new QueryWrapper<>();
        eduChapterQW.eq("course_id",courseId);
        baseMapper.delete(eduChapterQW);
    }
}
